Teach Yourself PHP with MySQL by Nat McBride
Author:Nat McBride [McBride, Nat]
Language: eng
Format: mobi
Publisher: Hodder Headline
Published: 2011-05-12T16:00:00+00:00
7.3 Putting it all together
Before we show the full script for this example, it’s worth a quick look at the structure using some pseudocode:
<?
// if the form has been posted, analyse it:
if ($_POST) {
foreach($_POST as $k => $v) {
$v = trim($v) ;
$$k = $v ;
}
$error = “” ;
// cleaning and validation code here...
// ...then display errors or results:
if ($error != “”) {
echo “$error <P>Please try again.” ;
} else {
echo “<P><B>Forename:</B> $forename<BR/>” ;
// etc...
}
// if the form has not been posted, display it!
} else {
?>
<FORM METHOD=”POST” ACTION=”<? echo $_SERVER[‘PHP_SELF’] ?>”>
<!— HTML to display form —>
<?
// remember to close the if... braces!
}
?>
Now for the full code listing:
<HTML>
<HEAD>
<LINK REL=”stylesheet” TYPE=”text/css” HREF=”plain.css” />
</HEAD>
<BODY>
<?
if ($_POST) {
foreach($_POST as $k => $v) {
$v = trim($v) ;
$$k = $v ;
}
// create empty error variable
$error = “” ;
// check for data in required fields
if (($forename == “”) ||
($surname == “”) ||
($email == “”)) {
$error = “Please fill in all the required fields.<BR/>” ;
}
// validate $age
if (is_numeric($age) == FALSE) {
$error = “Please enter a valid age (numbers only!)<BR/>” ;
}
// validate $email
if ((strpos($email, “@”) === FALSE) ||
(strpos($email, “.”) === FALSE) ||
(strpos($email, “ “) != FALSE) ||
(strpos($email, “@”) > strrpos($email, “.”))) {
$error .= “Please enter a valid email address<BR/>” ;
}
// clean and validate $tel
$notel = array(“+”, “ “, “(“, “)”, “[“, “]”, “-”, “,”, “#”) ;
$tel = str_replace($notel, array(“00”), $tel) ;
if (is_numeric($tel)== FALSE) {
$error .= “Please enter a valid telephone number<BR/>” ;
}
// clean $biog and add <br/>s
$biog = stripslashes($biog) ;
$biog = nl2br($biog) ;
if ($error != “”) {
echo “$error <P>Please hit the back button to try again.” ;
} else {
echo “<P><B>Forename:</B> $forename<BR/>” ;
echo “<B>Surname:</B> $surname<BR/>” ;
echo “<B>Age:</B> $age<BR/>” ;
echo “<B>Sex:</B> $sex<BR/>” ;
echo “<B>Email:</B> $email<BR/>” ;
echo “<B>Telephone:</B> $tel<BR/>” ;
echo “<B>Description:</B> $biog<BR/>” ;
}
} else {
?>
<TABLE BORDER=”0” CELLPADDING=”3” CELLSPACING=”0”>
<FORM METHOD=”POST” ACTION=”<? echo $_SERVER[‘PHP_SELF’] ?>”>
<TR>
<TD>Forename:</TD>
<TD><INPUT TYPE=”text” NAME=”forename” SIZE=”45” MAXLENGTH=”100”/></TD>
</TR><TR>
<TD>Surname:</TD>
<TD><INPUT TYPE=”text” NAME=”surname” SIZE=”45” MAXLENGTH=”100”/></TD>
</TR><TR>
<TD>Age:</TD>
<TD><INPUT TYPE=”text” NAME=”age” SIZE=”5” MAXLENGTH=”5”/></TD>
</TR><TR>
<TD>Sex:</TD>
<TD><INPUT TYPE=”radio” NAME=”sex” VALUE=”F”/>F
   <INPUT TYPE=”radio” NAME=”sex” VALUE=”M”/>M</TD>
</TR><TR>
<TD>Email:</TD>
<TD><INPUT TYPE=”text” NAME=”email” SIZE=”45” MAXLENGTH=”100”/></TD>
</TR><TR>
<TD>Telephone:</TD>
<TD><INPUT TYPE=”text” NAME=”tel” SIZE=”45” MAXLENGTH=”20”/></TD>
</TR><TR>
<TD COLSPAN=”2”>Describe yourself in a few words:</TD>
</TR><TR>
<TD COLSPAN=”2”><TEXTAREA NAME=”biog” ROWS=”4” COLS=”47”></TEXTAREA></TD>
</TR><TR>
<TD COLSPAN=”2”><INPUT TYPE=”SUBMIT” VALUE=”Send” /></TD>
</TR>
</FORM>
</TABLE>
<?
}
?>
</BODY>
</HTML>
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
The Mikado Method by Ola Ellnestam Daniel Brolund(27095)
Hello! Python by Anthony Briggs(25950)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(25286)
Kotlin in Action by Dmitry Jemerov(24396)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(23591)
Dependency Injection in .NET by Mark Seemann(23313)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(21945)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(20849)
Grails in Action by Glen Smith Peter Ledbrook(19869)
Adobe Camera Raw For Digital Photographers Only by Rob Sheppard(17073)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(16833)
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(14464)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(12584)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(11865)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(10650)
Hit Refresh by Satya Nadella(9239)
The Kubernetes Operator Framework Book by Michael Dame(8588)
Exploring Deepfakes by Bryan Lyon and Matt Tora(8446)
Robo-Advisor with Python by Aki Ranin(8390)